#!/ bin/sh

# Simple Unit Test class stub maker by Plan-B Software
# $Id: makeTestClasses.sh 53 2009-04-05 20:14:10Z planbsoftware@me.com $

make_dir()
{
	if [ "$1"X != "X" -a ! -d "$1" ]; then
		PARENT=`dirname $1`
		
		if [ $PARENT != $1 ]; then
			if [ ! -d $PARENT ]; then
				make_dir $PARENT
			fi
		fi

		mkdir "$1"
		if [ $? -ne 0 ]; then
			echo "mkdir failed for $1"
			exit 1
		fi
	fi
}

if [ $# -lt 1 ]; then
	echo $0 [sourcedir] [testdir]
	echo "Default testdir is named UnitTests"
	exit 1
fi

TEST_ROOT=UnitTests

if [ $2"X" != "X" ]; then
	TEST_ROOT=$2
fi 

for i in `find $1 -type f -name \*.m`
do
	SRC_CLASS=`basename $i | sed 's/.m/.h/'`
	DIR=$TEST_ROOT/`dirname $i | sed 's/..\/$1\///'`

	make_dir $DIR
	
	for CLASS in `cat $i | grep "@implementation" | sed 's/@implementation //' | sed 's/^\([a-zA-Z]*\).*/\1/' | sort -u`
	do
		MADE_NEW=
				
		# Test file
		TEST=$DIR/${CLASS}Test.m
		if [ ! -f $TEST ]; then
			MADE_NEW=$TEST
			( 
			echo "/**"
			echo " * \$Date: \$"
			echo " * \$Rev: 000 \$"
			echo " * \$Author: \$"
			echo " */" 
			echo
			echo "#import \"${SRC_CLASS}\""
			echo

			echo "#import <Foundation/Foundation.h>"
			echo "#import <SenTestingKit/SenTestingKit.h>"
			echo
			echo "@interface ${CLASS}Test : SenTestCase"
			echo "{"
			echo "}"
			echo "@end" 
			echo
			
			echo "@implementation ${CLASS}Test"
			echo
			echo "- (void)setUp"
			echo "{"
			echo "}"
			echo

			echo "- (void)tearDown"
			echo "{"
			echo "}"
			echo

			echo "//- (void)testSomething"
			echo "//{"
			echo "//	STAssertTrue( <#condition#>, @\"<#messagefmt#>\", <#va_args#> );"
			echo "//	STAssertNotNil( [<#obja#> <#method#>], @\"<#messagefmt#>\", <#va_args#> );"
			echo "//	STAssertEquals( <#scalar#>, <#scalar#>, @\"<#messagefmt#>\", <#va_args#> );"
			echo "//	STAssertEqualObjects( <#obja#>, <#objb#>, @\"<#messagefmt#>\", <#va_args#> );"
			echo "//	STAssertThrows( [<#obja#> <#method#>], @\"<#messagefmt#>\", <#va_args#> );"
			echo "//}"
			echo
			
			echo "@end" 
			) > $TEST
		fi
		
		if [ "$MADE_NEW"X != "X" ]; then
			echo $MADE_NEW
		fi
	done
done
